<?php

namespace Managers;

class Pollvotinghistory extends BaseManager
{
    protected $__table = '\Catalog\PollVotingHistory';
    protected $__tablequestions = '\Catalog\PollQuestions';


    public function getPoll($id)
    {

$res = $this->_em->createQueryBuilder()
                ->select('p','q')
                ->from($this->__table, 'p')
                ->join('p.question', 'q')
                ->where('p.question = :question')->setParameter('question', $id)
                ->andWhere('q.isdel = 0')
                ->getQuery()
                ->getArrayResult();
        return $res[0];
    }

    public function getQuestions()
        {
            $res = $this->_em->createQueryBuilder()
                    ->select('q')
                    ->from('\Catalog\PollQuestions', 'q')
                    ->where('q.isdel = 0')
                    ->getQuery()
                    ->getArrayResult();
            return $res;
    }
    public function getQuestionsId()
        {

            
            $res = $this->_em->createQueryBuilder()
                    ->select('q')
                    ->from('\Catalog\PollQuestions', 'q')
                    ->where('q.isdel = 0')
                    ->getQuery()
                    ->getArrayResult();
            return $res;
    }
    public function getPullResults($limit=5, $page=0){
        $qs = "SELECT p.name as pool_name, a.* FROM newspaper.poll_questions p left join newspaper.poll_answers a on p.id=a.question_id";
        $result =  $this->getEntityManager()->getConnection()->executeQuery($qs)->fetchAll();

        $nice_result = array();
        foreach($result as $item){
            if(!isset($nice_result[$item['question_id']])){
                $nice_result[$item['question_id']] = array();
                $nice_result[$item['question_id']]['pool_name'] = $item['pool_name'];
                $nice_result[$item['question_id']]['answers'] = array();
            }
            $nice_result[$item['question_id']]['answers'][] = $item;
        }

        return $nice_result;

    }

    public function getQuestion($id)
        {
            $res = $this->_em->createQueryBuilder()
                    ->select('q')
                    ->from('\Catalog\PollQuestions', 'q')
                    ->where('q.id = :id')->setParameter('id', $id)
                    ->andWhere('q.isdel = 0')
                    ->getQuery()
                    ->getArrayResult();
            return $res[0];
    }

        public function getAnswersByPoolId($id)
        {
            $res = $this->_em->createQueryBuilder()
                    ->select('a','q')
                    ->from('\Catalog\PollAnswers', 'a')
                    ->join('a.question', 'q')
                    ->where('a.question = :question')->setParameter('question', $id)
                    ->andWhere('a.isdel = 0')
                    ->getQuery()
                    ->getArrayResult();
            return $res;
        }
    public function getAllAnswers()
        {
            $res = $this->_em->createQueryBuilder()
                    ->select('q','a')
                    ->from('\Catalog\PollQuestions', 'q')
                    ->join('a.question', 'q')
                    ->andWhere('a.isdel = 0')
                    ->getQuery()
                    ->getArrayResult();
            return $res;

        }

    /**
     * @param  $id
     * @return
     */
    public function getPoolIdByAnswersId($id)
    {
            $res = $this->_em->createQueryBuilder()
                    ->select('a', 'q')
                    ->from('\Catalog\PollAnswers', 'a')
                    ->join('a.question', 'q')
                    ->where('a.id = '.$id)
                    ->getQuery()
                    ->getArrayResult();
            return $res[0];
    }
    public function addAnswers($id, array $answers)
    {

        foreach ($answers as $answer){
            $answ = new \Catalog\PollAnswers();
            $answ->setName($answer);
            $answ->setVotesCount(0);
            $answ->setQuestion($this->_em->getReference('\Catalog\PollQuestions', $id));
            $answ->setCreatedAt(new \DateTime());
            $answ->setUpdatedAt(new \DateTime());
            $answ->setIsdel(0);
            $this->_em->persist($answ);

        }
        $this->_em->flush();
        return $answ;

    }
    public function addQuestion($question,$isactive)
    {
        $poll = new \Catalog\PollQuestions();
        $poll->setName($question);
        $poll->setCreatedAt(new \DateTime());
        $poll->setUpdatedAt(new \DateTime());
        $poll->setIsdel(0);
        $poll->setIsactive($isactive);
        $this->_em->persist($poll);
        $this->_em->flush();
        return $poll;

    }
    public function addAnswer($id, $answer)
    {
            $answ = new \Catalog\PollAnswers();
            $answ->setName($answer);
            $answ->setVotesCount(0);
            $answ->setQuestion($this->_em->getReference('\Catalog\PollQuestions', $id));
            $answ->setCreatedAt(new \DateTime());
            $answ->setUpdatedAt(new \DateTime());
            $answ->setIsdel(0);
            $this->_em->persist($answ);

        $this->_em->flush();
        return $answ;

    }

    public function increase($id)
    {
        $object = $this->_em->find('\Catalog\PollAnswers', $id);
        $votesCount = $object->getVotesCount();
        $votesCount++;
        $object->setVotesCount($votesCount);
        $this->_em->persist($object);
        $this->_em->flush();

    }

    /**
     * @param  $id
     * @param  $answer_id
     * @param  $longIP
     * @return void
     */
    public function setVotingHistory($id,$answer_id,$longIP)
    {
        $object = new \Catalog\PollVotingHistory();
        $object->setQuestion($this->_em->getReference('\Catalog\PollQuestions', $id));
        $object->setAnswer($this->_em->getReference('\Catalog\PollAnswers', $answer_id));
        $object->setCreatedAt(new \DateTime());
        $object->setIp($longIP);
        $this->_em->persist($object);
        $this->_em->flush();

    }

    function delQuestion($id)
    {
        $object = $this->_em->find($this->__tablequestions, $id);
        if ($object) {
            $object->setIsdel(1);
            $object->setIsactive(0);

            $this->_em->persist($object);
            $this->_em->flush();
            return true;
        }
        return false;
    }
    function delAnswer($id)
    {
        $object = $this->_em->find('\Catalog\PollAnswers', $id);
        if ($object) {
            $object->setIsdel(1);

            $this->_em->persist($object);
            $this->_em->flush();
            return true;
        }
        return false;
    }
    public function editQuestion($id,$name,$isactive)
    {
        $object = $this->_em->find('\Catalog\PollQuestions', $id);
        $object->setName($name);
        $object->setUpdatedAt(new \DateTime());
        $object->setIsactive($isactive);
        $this->_em->persist($object);
        $this->_em->flush();

    }
    public function getAnswers($id)
    {
            $res = $this->_em->createQueryBuilder()
                    ->select('q')
                    ->from('\Catalog\PollAnswers', 'q')
                    ->where('q.question = :question')->setParameter('question', $id)
                    ->andWhere('q.isdel = 0')
                    ->getQuery()
                    ->getArrayResult();
            return $res;
    }
    public function getAnswer($id)
    {
            $res = $this->_em->createQueryBuilder()
                    ->select('a')
                    ->from('\Catalog\PollAnswers', 'a')
                    ->where('a.id = '.$id)
                    ->getQuery()
                    ->getArrayResult();
            return $res[0];
    }
    public function editAnswer($answers)
    {
        foreach ($answers as $key=>$answer) {

            $object = $this->_em->find('\Catalog\PollAnswers', $key);
            $object->setName($answer);
            $object->setUpdatedAt(new \DateTime());
            $this->_em->persist($object);
        }
        $this->_em->flush();

    }
    public function checkIP($ip, $id)
        {

            $res = $this->_em->createQueryBuilder()
                    ->select('h')
                    ->from($this->__table, 'h')
                    ->where('h.ip = '.$ip)
                    ->andWhere('h.question = '.$id)
                    ->getQuery()
                    ->getArrayResult();

            return $res;
    }
    public function getActivePollId()
        {
            $res = $this->_em->createQueryBuilder()
                    ->select('q.id')
                    ->from('\Catalog\PollQuestions', 'q')
                    ->where('q.isdel = 0')
                    ->andWhere('q.isactive = 1')
                    ->getQuery()
                    ->getArrayResult();
            return $res;
    }

}
